package com.hs.biye.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.model.DTO.MedicineRequest;
import com.hs.biye.model.DTO.PatientRequest;
import com.hs.biye.model.VO.EmployeeVO;
import com.hs.biye.model.VO.PatientVo;
import com.hs.biye.model.entity.*;
import com.hs.biye.model.entity.Patient;
import com.hs.biye.model.enums.GenderEnum;
import com.hs.biye.model.reslut.Result;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.PatientService;
import com.hs.biye.mapper.PatientMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author haoshuai
* @description 针对表【patient】的数据库操作Service实现
* @createDate 2024-01-15 19:06:20
*/
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>
    implements PatientService{

    @Resource
    private PatientMapper patientMapper;
    /**
     * 分页查询
     * @param
     * @param
     * @return
     */
    @Override
    public ResultPage<PatientVo> getPagePatient(PatientRequest patientRequest) {
        Integer page = patientRequest.getPages();
        Integer size = patientRequest.getSize();

        if (page == null || size == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }

        IPage<Patient> page2 = new Page<>(page, size);
        patientMapper.selectPage(page2,getQueryWrapper(patientRequest));
        List<Patient> records = page2.getRecords();
        long total = page2.getTotal();
        long current = page2.getCurrent();
        long pages = page2.getPages();

        List<PatientVo> list = new ArrayList<>();

        records.forEach(item -> {
            PatientVo patientVo = new PatientVo();
            BeanUtils.copyProperties(item, patientVo);
            patientVo.setGender(GenderEnum.getValueByCode(item.getGender()));
            list.add(patientVo);

        });
        return ResultPage.success(list, total,current,pages);
    }

    @Override
    public Boolean deletepatient(Integer id) {
        if (id == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        return removeById(id);
    }


    //没有添加就诊患者，有登录
    @Override
    public PatientVo addPatient(PatientRequest patientRequest) {
        if (patientRequest == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        String patientName = patientRequest.getPatientName();
        String identity = patientRequest.getIdentity();
        String phone = patientRequest.getPhone();


        //查询
        Patient patient = baseMapper.selectOne(new LambdaQueryWrapper<Patient>()
                .eq(Patient::getPatientName, patientName)
                .eq(Patient::getIdentity, identity)
                .eq(Patient::getPhone, phone));

        //如何用户已经存在，直接返回
        PatientVo patientVo = new PatientVo();
        if (patient != null){
            BeanUtils.copyProperties(patient,patientVo);
            return patientVo;
        }
        //不存在添加患者信息
        patient = new Patient();
        patient.setPatientName(patientName);
        patient.setIdentity(identity);
        patient.setPhone(phone);
        boolean save = save(patient);
        if (!save){
            throw new BaseException(MessageConstant.PAR_NOT);
        }
        BeanUtils.copyProperties(patient,patientVo);
        return patientVo;
    }

    private LambdaQueryWrapper<Patient> getQueryWrapper(PatientRequest patientRequest){

        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        String patientName = patientRequest.getPatientName();
        String identity = patientRequest.getIdentity();
        Integer gender = patientRequest.getGender();
        String phone = patientRequest.getPhone();
        queryWrapper.eq(ObjectUtil.isNotEmpty(patientName),Patient::getPatientName,patientName);
        queryWrapper.eq(ObjectUtil.isNotEmpty(identity),Patient::getIdentity,identity);
        queryWrapper.eq(ObjectUtil.isNotEmpty(gender),Patient::getGender,gender);
        queryWrapper.eq(ObjectUtil.isNotEmpty(phone),Patient::getPhone,phone);
        //排序
        // employeeQueryWrapper.orderBy(true,true,Employee::getCreateTime);
        return queryWrapper;
    }
}




